package com.example.qimo;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

public class newnote extends AppCompatActivity {

    private final String Tag="newnote";
    DBhelper myDb;

    private Button btnSave;
    private EditText titleEditText;
    private EditText textEditText;
    private TextView timeTextView;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newnote);

        myDb = new DBhelper(this);
        SQLiteDatabase db = myDb.getReadableDatabase();
        titleEditText = findViewById(R.id.title);
        textEditText = findViewById(R.id.text);
        timeTextView = findViewById(R.id.time);
        btnSave = findViewById(R.id.submit);

        if(timeTextView.getText().length()==0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            Log.i(Tag,"当前时间" + simpleDateFormat.format(date));
            timeTextView.setText(""+simpleDateFormat.format(date));
        }

    }
    public void onClickback(View btn){
       finish();
    }
    public void onsubmit(View btn){
        //将数据保存到数据库

        String title = titleEditText.getText().toString();
        String text =textEditText.getText().toString();
        String time= timeTextView.getText().toString();


        SQLiteDatabase db = myDb.getWritableDatabase();  //数据库读写
        ContentValues values = new ContentValues();
        Cursor cursor = db.query(DBhelper.TB_NAME,null,null,
                null,null,null,null);
        //数据非空判断
        if("".equals(titleEditText.getText().toString())){
            Toast.makeText(newnote.this,"标题不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if("".equals(textEditText.getText().toString())) {
            Toast.makeText(newnote.this,"内容不能为空",Toast.LENGTH_LONG).show();
            return;
        }

        values.put(DBhelper.TITLE,title);
        values.put(DBhelper.CONTENT,text);
        values.put(DBhelper.TIME,time);
//        Log.i(Tag,"保存时间" + cursor.getCount()+1);
        db.insert(DBhelper.TB_NAME,null,values);
        Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        Log.i(Tag,"保存时间" + simpleDateFormat.format(date));
        Toast.makeText(newnote.this,"保存时间: "+simpleDateFormat.format(date),Toast.LENGTH_LONG).show();


        db.close();
        finish();
    }


}
